#!/bin/bash

#添加host记录
export hostname=`hostname`
export ip=`ip a |grep 10.0 |grep inet |awk '{print $2}' |awk -F "/" '{print $1}'`
echo "${ip}    ${hostname}" >>/etc/hosts
echo "10.50.9.216    hb.sincerecloud.com" >>/etc/hosts

#配置镜像源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# centos8 更换镜像源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

#添加kubernetes镜像源
tee  /etc/yum.repos.d/kubernetes.repo <<-'EOF'
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum clean all
yum makecache

#安装工具包
yum install -y wget util-linux socat conntrack-tools net-tools ntpdate ipset ipvsadm chrony vim  net-tools bash-completion lrzsz yum-plugin-versionlock yum-utils device-mapper-persistent-data lvm2
systemctl enable chronyd
systemctl start chronyd
chronyc sources

#关闭swap
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

#禁用防火墙
systemctl stop firewalld && systemctl disable firewalld && systemctl status firewalld && firewall-cmd --state
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT

#禁用seLinux
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
setenforce 0

#修改内核参数
tee /etc/sysctl.d/k8s.conf <<-'EOF'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness = 0
net.ipv4.ip_forward = 1
EOF
sysctl -p
sysctl --system

#加载内核模块
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
modprobe br_netfilter

#加载IPvs
tee /etc/sysconfig/modules/ipvs.modules <<-'EOF'
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4

#contos 8替换上边这行为下边的
modprobe -- nf_conntrack
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

#contos8修改并锁定resolv.conf文件
nameserver 10.250.21.11
nameserver 10.250.21.12
chattr +i /etc/resolv.conf 

#安装docker 
yum install -y docker-ce

#安装cri-docker
wget http://qiniu.thinkleak.com/cri-dockerd-0.3.1-3.el7.x86_64.rpm
rpm -Uvh --force cri-dockerd-0.3.1-3.el7.x86_64.rpm
sed -i 's/fd:\/\//fd:\/\/ --pod-infra-container-image registry.k8s.io\/pause:3.9/g' /usr/lib/systemd/system/cri-docker.service

#安装NGINX并启用stream模块
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install epel-release
yum install nginx
yum install nginx-mod-stream --skip-broken
#安装所有模块
yum -y install nginx-all-modules.noarch --skip-broken

#安装containerd
yum install -y containerd.io-1.6.15-3.1.el7.x86_64
#centos8安装
yum install -y containerd.io-1.6.15-3.1.el8.x86_64
#配置containerd
containerd config default > /etc/containerd/config.toml
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#' /etc/containerd/config.toml
sed -i 's#sandbox_image = "registry.k8s.io/pause:3.6"#sandbox_image = "hb.sincerecloud.com/k8s/v1.26.3/pause:3.9"#' /etc/containerd/config.toml
systemctl enable containerd
systemctl start containerd
systemctl status containerd
echo "runtime-endpoint: unix:///run/containerd/containerd.sock" >/etc/crictl.yaml
echo "image-endpoint: unix:///run/containerd/containerd.sock" >>/etc/crictl.yaml
#安装配置cri-o 手动vim 替换
export OS=CentOS_7
export VERSION=1.25
curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable.repo https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/devel:kubic:libcontainers:stable.repo
curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.repo https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/devel:kubic:libcontainers:stable:cri-o:$VERSION.repo
yum install cri-o -y

sed -i '479s/$/pause_image = "dyrnq\/pause:3.9"/' /etc/crio/crio.conf
echo "runtime-endpoint: unix:///var/run/crio/crio.sock" >/etc/crictl.yaml
echo "image-endpoint: unix:///var/run/crio/crio.sock" >>/etc/crictl.yaml

#创建kubelet启动环境变量读取目录
mkdir -p /usr/lib/systemd/system/kubelet.service.d

#创建kubelet启动所使用的环境变量文件
tee /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf <<-'EOF'
# Note: This dropin only works with kubeadm and kubelet v1.11+
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/sysconfig/kubelet
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS
EOF

#配置hub证书用于拉取镜像
echo >> /etc/pki/ca-trust/extracted/p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LlH0uvznQPg6e8lfjAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUA
A4IBAQB57ab0fzPGG0HV9u7ZvvqTyPaoxV+UCTbSQwMz23fb9clcZCLv1j+K9tzi
0LG7modJdyr3rEg1wn8BYF0Qnjvba24iDekAozqCwMvS/cX3t/lNmWPaMzNLWj5L
TtR0I++FjBJ2Zcc7pjiRUq/a0x/8JvlsBxmeTuiEVPrzUBslT08wFX/4JrEmvkms
Q9yfzJ0xTE1YBEU8bvoECrL9azQIC107O0sPvJJSL7GbcQs6MgCCK9mULN4uRdRd
ybiwMlCC5ocNsNuYe5p87su3YrxlJ+NB+s49fAT8hvNju8v2oSbcdzlvaBp9Vnbh
qA/1ZxEv44TNI4ekZ/IUaa9Trl7D
-----END CERTIFICATE-----
EOF

# kubectl命令自动补全
# 即可使用
source /usr/share/bash-completion/bash_completion
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
# 日后永久使用
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc

#列出kubectl可用的版本
yum list kubectl --showduplicates | sort -r
#下载kubeadm kubectl kubelet
yum install -y kubelet-1.26.3-0 cri-tools-1.26.0-0 kubectl-1.26.3-0

#锁定版本防止被意外更新导致集群出问题
yum versionlock cri-tools kubelet kubectl

#kubelet开机启动
systemctl enable kubelet

# 拉取镜像
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/kube-apiserver:v1.26.3
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/kube-scheduler:v1.26.3
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/kube-controller-manager:v1.26.3
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/kube-proxy:v1.26.3
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/etcd:3.5.6-0
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/pause:3.9
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/coredns:v1.9.3
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/k8s/v1.26.3/kube-keepalived:v3.10
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/calico/v3.24.1/node:v3.24.1
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/calico/v3.24.1/cni:v3.24.1
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/calico/v3.24.1/kube-controllers:v3.24.1
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/calico/v3.24.1/typha:v3.24.1
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/kubernetesui/metrics-scraper:v1.0.8
crictl -r unix:///var/run/crio/crio.sock pull hb.sincerecloud.com/kubernetesui/dashboard:v2.7.0